package com.base.controller.utils.CZB;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Maps;
import com.base.helper.WebHelper;
import com.base.po.czb_sdk.CZBUtil;

import java.math.BigDecimal;
import java.util.Map;

public class CZBPayUtils {
    private static String APP_KEY = "appm_api_h598638374";
    private static String APP_SECRET = "c42bde795fe5e6c4992a691b94f59f31";
    private static String PLATFORMTYPE = "98638374";
//    private static String APP_KEY = "suibiandah51.0";
//    private static String APP_SECRET = "12ba273f41c9ac54e7303c0af5d80136";

//    private static String PLATFORMTYPE = "92656163";

    private static String PAY_CODE = "1054";

    private static String APP_KEY_API = "suibianda1.0";
    private static String APP_SECRET_API = "16bc2edcaed84b3eaab079a16a4eeb54";
    private static String PLATFORMTYPE_API = "92654416";
    private static String PAY_CODE_API = "1054";

    /**
     * 正式环境
     */
    private static String FORMAL_CONTEXT = "https://mcs.czb365.com/";
    /**
     * 测试环境
     */
    private static String TEST_CONTEXT = "https://test-mcs.czb365.com/";
    /**
     * 使用环境
     */
    private static String USE_CONTEXT = FORMAL_CONTEXT;

    private static String CZB_TOKEN_LOGIN = USE_CONTEXT + "services/v3/begin/platformLoginSimpleAppV4";// 平台授权登录
    private static String CZB_GAS_INFO = USE_CONTEXT + "services/v3/gas/queryGasInfoListOilNoNew";// 初始化油站数据
    private static String CZB_GAS_PRICE = USE_CONTEXT + "services/v3/gas/queryPriceByPhone";// 根据用户查询油站状态和油价
    private static String CZB_GEMERATE_ORDER = USE_CONTEXT + "services/v3/orderws/generateOrder";//  订单推送接口
    private static String CZB_CANCEL_ORDER = USE_CONTEXT + "services/v3/orderws/cancelOrder";// 订单取消接口
    private static String CZB_PAY_CALLBACK = USE_CONTEXT + "services/v3/payCallbackApi";//  支付回告接口
    private static String CZB_PAY_REFUND = USE_CONTEXT + "services/v3/orderws/applyRefund";//  申请退款接口
    private static String CZB_PAY_ORDER_READ = USE_CONTEXT + "services/v3/order/platformOrderInfo";//  申请退款接口

    /**
     *  @Date: 2019/12/26 15:31
     *  @Description:
     *  调起H5接口的接口
     */
    public static String getCZBH5Url(String mobile, String gasId, String oilGun) {
        String code= CZBTokenUtils.getCzbPaySecretcode(mobile);
        JSONObject secretCode= JSONObject.parseObject(code);
        if (secretCode.getString("code").equals("200")){
            String secretCodeString=secretCode.getString("result");
            String CZB_H5_URL = "https://open.czb365.com/redirection/todo/?platformType="+PLATFORMTYPE+"&authCode="+secretCodeString+"&gasId="+gasId+"&gunNo="+oilGun;
            return CZB_H5_URL;
        }
        return null;
    }

    public static String createToken(Long dateTime, String mobile) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("platformType", PLATFORMTYPE);
        map.put("platformCode", mobile);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY);
        String sign = CZBUtil.getSign(map, APP_SECRET);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_TOKEN_LOGIN, param, null, null);

        return result;
    }
    public static String createTokenAPI(Long dateTime, String mobile) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("platformType", PLATFORMTYPE_API);
        map.put("platformCode", mobile);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY_API);
        String sign = CZBUtil.getSign(map, APP_SECRET_API);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_TOKEN_LOGIN, param, null, null);

        return result;
    }
    public static String getAllGasInfo(Long dateTime, String token) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("channelId", PLATFORMTYPE);
        map.put("timestamp", dateTime);
        map.put("token", token);
        map.put("app_key", APP_KEY);
        String sign = CZBUtil.getSign(map, APP_SECRET);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_GAS_INFO, param, null, null);

        return result;
    }

    public static String getGasOrder(Long dateTime, String token, String gasId, Integer gunNo, BigDecimal amountGun,
                                     String outerOrderId, BigDecimal priceGun, BigDecimal priceUnit, BigDecimal litre) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("gasId", gasId);
        map.put("gunNo", gunNo);
        map.put("amountGun", amountGun);
        map.put("outerOrderId", outerOrderId);
        map.put("priceGun", priceGun);
        map.put("priceUnit", priceUnit);
        map.put("litre", litre);
        map.put("timestamp", dateTime);
        map.put("token", token);
        map.put("app_key", APP_KEY_API);
        String sign = CZBUtil.getSign(map, APP_SECRET_API);
        String param = CZBUtil.getParam(map, sign);
        System.out.println("sign:" + sign);
        System.out.println("param:" + param);
        String result = WebHelper.PostData(CZB_GEMERATE_ORDER, param, null, null);
        return result;
    }

    public static String getPayCallBack(Long dateTime, String orderId, Integer payAmount, Integer payStatus) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("orderId", orderId);
        map.put("payAmount", payAmount);
        map.put("payStatus", payStatus);
        map.put("payCode", PAY_CODE_API);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY_API);
        String sign = CZBUtil.getSign(map, APP_SECRET_API);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_PAY_CALLBACK, param, null, null);
        return result;
    }

    public static String getPayRefund(Long dateTime, String token, String orderSn, String orderId, String outOrderId,
                                      Integer refundAmount) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("orderSn", orderSn);
        map.put("orderId", orderId);
        map.put("outOrderId", outOrderId);
        map.put("refundAmount", refundAmount);
        map.put("timestamp", dateTime);
        map.put("token", token);
        map.put("app_key", APP_KEY_API);
        String sign = CZBUtil.getSign(map, APP_SECRET_API);
        String param = CZBUtil.getParam(map, sign);
        System.out.println("param:" + param);
        System.out.println("url:" + CZB_PAY_REFUND);
        String result = WebHelper.PostData(CZB_PAY_REFUND, param, null, null);
        return result;
    }

    public static String getQueryOrder(Long dateTime, String token, String paySn, String orderId, String outerOrderId,
                                       String orderSource) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("orderSource", orderSource);
        map.put("paySn", paySn);
        map.put("orderId", orderId);
        map.put("outerOrderId", outerOrderId);
        map.put("timestamp", dateTime);
        map.put("token", token);
        map.put("app_key", APP_KEY);
        String sign = CZBUtil.getSign(map, APP_SECRET);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_PAY_REFUND, param, null, null);
        return result;
    }

    public static String getPlatformOrder(Long dateTime, String beginTime, String endTime, Integer pageIndex, Integer pageSize) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        map.put("orderSource", PLATFORMTYPE);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY);
        String sign = CZBUtil.getSign(map, APP_SECRET);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_PAY_ORDER_READ, param, null, null);
        return result;
    }

    public static String getPlatformOrderApi(Long dateTime, String beginTime, String endTime, Integer pageIndex, Integer pageSize) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        map.put("orderSource", PLATFORMTYPE_API);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY_API);
        String sign = CZBUtil.getSign(map, APP_SECRET_API);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_PAY_ORDER_READ, param, null, null);
        return result;
    }

    public static String getCzbPayOrderRead(Long dateTime,String paySn,String beginTime,String endTime,Integer orderStatus,
                                            String orderId,String phone,Integer pageIndex,Integer pageSize){
        Map<String, Object> map = Maps.newHashMap();
        map.put("orderSource", PLATFORMTYPE);
        map.put("paySn", paySn);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("orderStatus", orderStatus);
        map.put("orderId", orderId);
        map.put("phone", phone);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        map.put("timestamp", dateTime);
        map.put("app_key", APP_KEY);
        String sign = CZBUtil.getSign(map, APP_SECRET);
        String param = CZBUtil.getParam(map, sign);
        String result = WebHelper.PostData(CZB_PAY_ORDER_READ, param, null, null);
        return result;
    }
}
